// pages/addAnniversary/addAnniversary.js
let app=getApp();
Page({
  data: {
    date:"选择日期",
    anniversaryTitle:null,
    anniversaryId:null,
    openid:app.globalData.openid,
    jinname:"",
  },
  //获取标题
  getAnniversaryTitle(e)
  {
    this.data.anniversaryTitle=e.detail;
  },
//获取日期
bindDateChange: function(e) {
  this.setData({
    date: e.detail.value
  });
},
//生成纪念日id
createAnniversaryId()
{
  var result = "";
  var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var charactersLength = characters.length;
  for (var i = 0; i < 5; i++) {
    result += characters.charAt(
      Math.floor(Math.random() * charactersLength)
    );
  }
  let dat=this.data.date;
    this.data.anniversaryId=result+dat.replace(/-/g, "");
    console.log(this.data.anniversaryId);
},
getXiaoxi()
  {
    let temId = 'E5hcsS32U9hqWHtqjocQ5lXOOjOlV97Wc26vNOSBu34' // 小程序配置模板信息-模板ID
wx.getSetting({
	withSubscriptions: true,//是否同时获取用户订阅消息的订阅状态，默认不获取
	success: (res)=> {
		console.log(res)
		if (res.subscriptionsSetting && res.subscriptionsSetting.itemSettings &&
			res.subscriptionsSetting.itemSettings[temId] == "reject"){
			//打开设置去设置
			this.openConfirm('检测到您没打开推送权限，是否去设置打开？')
		}else {
			wx.requestSubscribeMessage({
				tmplIds: [temId],
				success: (res)=> {
					if (res[temId] == 'accept'){
            wx.showToast({
              title: '订阅成功！',
              icon:"success"
            })
					}
				},
				fail: (res)=> { console.info(res) },
			})
		}
	}
})
  },
//添加日期
addAnniversary(){
  let that=this;
  that.getXiaoxi();
  setTimeout(this.sendAdd,2000);
},
//发送添加信息
sendAdd()
{
  let that=this;
  that.createAnniversaryId();
  let datestr="";
  datestr=that.data.date;
  let dateArray=datestr.split("-");
  let year=dateArray[0];
  let month=dateArray[1];
  let day=dateArray[2];
  wx.request({
    url:app.globalData.url+'/addjinian', 
    header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
    data: {
      openid:that.data.openid,
      jinid:that.data.anniversaryId,
      jninfo:that.data.anniversaryTitle,
      jndate:that.data.date,
      month:month,
      day:day,
      hour:8,
      minute:0
     },
     method: 'post',
     success: function (res) {
          console.log("成功");
          wx.navigateBack({
            delta: 1,
          })
     },
     fail:function(err){//请求失败之后要做什么
        console.log('请求失败')
        }
        
})
}
})